'use strict';

const { Service } = require('ee-core');
const Storage = require('ee-core/storage');

/**
 * SureBetSqliteDbService
 * @class
 */
class SureBetSqliteDbService extends Service {

    constructor(ctx) {
        super(ctx);
        this.sqliteFile = 'surebet.db';
        // sqlite数据库
        let sqliteOptions = {
            driver: 'sqlite',
            default: {
                timeout: 6000,
                verbose: console.log // 打印sql语法
            }
        }
        this.surebetDb = Storage.connection(this.sqliteFile, sqliteOptions);
    }

    /**
     * 查询配置信息
     */
    async getDbValueByKey(key) {
        const selectConfig = this.surebetDb.db.prepare(`SELECT * FROM sys_config WHERE key = @key order by create_at desc`);
        let rows = selectConfig.get({key: key});
        return rows.value
    }

    /**
     * 查询配置信息
     */
    async saveDbConfig(data) {
        const insert = this.surebetDb.db.prepare(`INSERT INTO sys_config (key, value,update_at,create_at) VALUES (@key, @value,@createAt,@updateAt)`);
        insert.run(data);
        return true
    }


    /**
     * 更新配置信息
     */
    async updateDbConfig(data) {
        const insert = this.surebetDb.db.prepare(`update  sys_config  set value = @value where key = @key`);
        insert.run(data);
        return true
    }

}

SureBetSqliteDbService.toString = () => '[class SureBetSqliteDbService]';
module.exports = SureBetSqliteDbService;